package com.fulin.datastract.array;

import java.lang.reflect.Array;

import org.springframework.util.Assert;

/**
*
* @description 用数组模拟有界队列
* @auther fulin16@crnet.com.hk
* @since 2020年5月30日 下午2:39:18
*
*/
@SuppressWarnings("all")
public class ArrayQueue<T> {
	
	private T[] arr;
	private int head;
	private int tail;
	private int lenth;

	public ArrayQueue(int maxSize,Class<T> clazz) {
		lenth = maxSize;
		arr = (T[])Array.newInstance(clazz, maxSize);
		head = -1;
		tail = -1;
	}
	
	public boolean isFull() {
		return tail == lenth -1;
	}
	
	public boolean isEmpty() {
		return tail == head;
	}
	
	public T add(T t) {
		Assert.isTrue(!isFull(), "队列已满,不能添加新的元素");
		tail ++;
		arr[tail] = t;
		return t;
	}
	
	public T get() {
		Assert.notEmpty(arr);
		return arr[head];
	}
}
